package com.zhongfl.crm.service.impl;

import com.zhongfl.crm.enums.LxMessageType;
import com.zhongfl.crm.mapper.LxDepartmentMapper;
import com.zhongfl.crm.model.LxDepartment;
import com.zhongfl.crm.model.boss.Amiba;
import com.zhongfl.crm.service.LxDepartmentService;
import com.zhongfl.crm.service.LxMessageLogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import tech.heptagram.core.common.util.JsonUtil;
import tech.heptagram.staff.cas.common.AssertUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 向日葵
 * @Date 2018/9/20
 */
@Service
public class LxDepartmentServiceImpl implements LxDepartmentService {

    @Resource
    private LxDepartmentMapper lxDepartmentMapper;

    @Resource
    private LxMessageLogService lxMessageLogService;

    @Override
    public List<LxDepartment> findByParentId(Long parentId) {
        return lxDepartmentMapper.selectByParentId(parentId);
    }

    @Override
    public LxDepartment findByLxId(Long lxId) {
        AssertUtils.notNull(lxId, "励销id不能为空");
        return lxDepartmentMapper.selectByLxId(lxId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertDepartment(LxDepartment lxDepartment) {
        lxDepartmentMapper.insertDepartment(lxDepartment);

        String content = JsonUtil.toJson(lxDepartment);
        lxMessageLogService.save(LxMessageType.DEPARTMENT_CREATED, String.valueOf(lxDepartment.getLxId()), content);
    }
    @Override
    public void deletedDepartmentById(Long lxId) {
        lxDepartmentMapper.deletedDepartmentById(lxId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDepartmentById(LxDepartment lxDepartment) {
        AssertUtils.notNull(lxDepartment, "新增部门信息不能为空");
        AssertUtils.notNull(lxDepartment.getLxId(), "励销id不能为空");
        lxDepartmentMapper.updateById(lxDepartment);
    }

    @Override
    public List<LxDepartment> findAllDepartment() {
        List<LxDepartment> list = new ArrayList<>();
        //部门分组排序
        List<LxDepartment> lxDepartments = lxDepartmentMapper.selectAllDepartment();
        for (LxDepartment lxDepartment : lxDepartments) {
            if (lxDepartment.getLevel() == 0){
                list.add(lxDepartment);
            }else if(lxDepartment.getLevel() == 1){
                list.add(lxDepartment);
                for (LxDepartment dept : lxDepartments) {
                    if(dept.getParentId().equals(lxDepartment.getLxId())){
                        list.add(dept);
                    }
                }
            }
        }
        return list;
    }

    /**
     * 将所有匹配状态改为'NO'
     */
    @Override
    public void setAllMatchStatusToNo() {
        lxDepartmentMapper.updateAlltoNo();
    }

    /**
     * 逻辑删除所有未匹配成功的信息
     */
    @Override
    public void deleteNoMatch() {
        lxDepartmentMapper.deleteAllNo();
    }

    /**
     * 根据名称查询阿米巴
     *
     * @param name 名称
     * @return 阿米巴
     */
    @Override
    public Amiba findAmibaByName(String name) {
        return lxDepartmentMapper.selectAmibaByName(name);
    }

    /**
     * 修改匹配阿米巴
     *
     * @param lxDepartment 匹配阿米巴信息
     */
    @Override
    public void updateMatchAmiba(LxDepartment lxDepartment) {
        AssertUtils.notNull(lxDepartment, "励销部门信息为空，修改匹配阿米巴失败！");
        AssertUtils.notNull(lxDepartment.getId(), "励销部门ID为空，修改匹配阿米巴失败！");
        AssertUtils.notNull(lxDepartment.getAmibaId(), "匹配阿米巴ID为空，修改匹配阿米巴失败！");
        AssertUtils.notNull(lxDepartment.getAmibaName(), "匹配阿米巴名称为空，修改匹配阿米巴失败！");
        lxDepartmentMapper.updateMatchAmiba(lxDepartment.getId(), lxDepartment.getAmibaId(), lxDepartment.getAmibaName());
    }
}
